package com.shanyu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.GeneratedValue;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.shanyu.dao.ICouponDao;
import com.shanyu.dao.IItemDao;
import com.shanyu.dao.IItemPropDao;
import com.shanyu.dao.IItemSkuDao;
import com.shanyu.dao.ISkuDao;
import com.shanyu.dao.IUserCouponDao;
import com.shanyu.dto.CouponDto;
import com.shanyu.dto.ItemDto;
import com.shanyu.eo.CouponEo;
import com.shanyu.eo.ItemEo;
import com.shanyu.eo.ItemPropEo;
import com.shanyu.eo.ItemSkuEo;
import com.shanyu.eo.SkuEo;
import com.shanyu.eo.UserCouponEo;
import com.shanyu.service.IItemService;

@Service
public class ItemServiceImpl implements IItemService{
	
	@Autowired
	private IItemDao itemDao;
	
	@Autowired
	private IItemSkuDao itemSkuDao;
	
	@Autowired
	private ISkuDao skuDao;
	
	@Autowired
	private IItemPropDao itemPropDao;
	@Autowired
	private ICouponDao couponDao;
	
	@Autowired
	private IUserCouponDao userCouponDao;

	public ItemDto getDetail(String itemId, String memberId) {
		// TODO Auto-generated method stub
		ItemDto dto = new ItemDto();
		
		ItemEo eo = itemDao.getById(itemId);
		if(eo != null){
			BeanUtils.copyProperties(eo, dto);
		}
		dto.setPropList(getItemProp(itemId));
		dto.setSkuList(getSku(itemId));
		if(eo.getIsRelation() == 0){
			dto.setCouponList(couponList(itemId,memberId));
		}
		return dto;
	}
	
	
	private List<SkuEo> getSku(String itemId){
		ItemSkuEo query = new ItemSkuEo();
		query.setItemId(itemId);
		List<ItemSkuEo> list = itemSkuDao.select(query);
		List<String> skuIds = new ArrayList<String>();
		if(!CollectionUtils.isEmpty(list)){
			for(ItemSkuEo eo : list){
				skuIds.add(eo.getSkuId());
			}
		}
		if(!CollectionUtils.isEmpty(skuIds)){
			return skuDao.selectBySkuIds(skuIds);
		}
		return null;
	}
	
	private List<ItemPropEo> getItemProp(String itemId){
		ItemPropEo query = new ItemPropEo();
		query.setItemId(itemId);
		return itemPropDao.select(query);
	}
	
	private List<CouponDto> couponList(String itemId,String memberId){
		List<CouponDto> res = new ArrayList<CouponDto>();
		
		CouponEo query = new CouponEo();
		query.setSendBeginTime(new Date());
		query.setSendEndTime(new Date());
		List<CouponEo> coupon = couponDao.select(query);
		
		if(!CollectionUtils.isEmpty(coupon)){
			for(CouponEo eo : coupon){
				CouponDto dto = new CouponDto();
				if(1 == eo.getType()){
					BeanUtils.copyProperties(eo, dto);
					dto.setIsValid(validCoupon(eo));
					dto.setIsHaving(havedCoupon(eo.getId(),memberId));
					res.add(dto);
				}else if(2 == eo.getType()){
					String catalogId = null;
					ItemEo item = itemDao.getById(itemId);
					if(item != null){
						catalogId = item.getCatalogId();
						if(catalogId.equals(eo.getCatalogId())){
							BeanUtils.copyProperties(eo, dto);
							dto.setIsValid(validCoupon(eo));
							dto.setIsHaving(havedCoupon(eo.getId(),memberId));
							res.add(dto);
						}
					}
				}else if(3 == eo.getType()){
					if(itemId.equals(eo.getItemId())){
						BeanUtils.copyProperties(eo, dto);
						dto.setIsValid(validCoupon(eo));
						dto.setIsHaving(havedCoupon(eo.getId(),memberId));
						res.add(dto);
					}
				}
			}
		}
		return res;
	}
	
	private int havedCoupon(String couponId,String memberId){
		if(StringUtils.isEmpty(memberId)){
			return 2;
		}else{
			UserCouponEo query = new UserCouponEo();
			query.setCouponId(couponId);
			query.setMemberId(memberId);
			List<UserCouponEo> list = userCouponDao.select(query);
			if(CollectionUtils.isEmpty(list)){
				return 1;
			}else{
				return 2;
			}
		}
	}
	
	private int validCoupon(CouponEo coupon){
		if(coupon.getValidBeginTime().before(new Date())){
			if(coupon.getValidEndTime().after(new Date())){
				return 1;
			}else{
				return 2;
			}
		}else{
			return 2;
		}
	}
	
	

}
